<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Biz extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Bizuser_model');
    }

    public function login() {
        $this->load->view('biz/login');
    }

    public function dologin() {
        if ($this->checkLogin()) {
            header('Location: /');
            return true;
        }

        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        $username = textFilter($username);
        $password = textFilter($password);
        if (!$username || !$password) {
            header('Location: /biz/login/');
            return false;
        }

        $bizUser = $this->Bizuser_model->getBizUserByName($username);
        if (!$bizUser) {
            header('Location: /biz/login/');
            return false;
        }

        if (!password_verify($password, $bizUser['password'])) {
            header('Location: /biz/login/');
            return false;
        }

        $_SESSION['bizUser']['username'] = $bizUser['username'];
        $_SESSION['bizUser']['userId']   = $bizUser['id'];

        header('Location: /');
        return true;
    }
}
